package user

import (
	"context"
	user_pb "mxshop/api/user/v1"
	"mxshop/app/mxshop_api/admin/internal/data/userSrv"
	doUser "mxshop/app/mxshop_api/admin/internal/domain/do/user"
	metav1 "mxshop/gmicro/pkg/common/meta/v1"
	"mxshop/gmicro/pkg/common/time"
)

type users struct {
	uc user_pb.UserClient
}

func (u *users) List(ctx context.Context, orderby []string, opts metav1.ListMeta) (*doUser.UserDOList, error) {
	var userList doUser.UserDOList

	pageInfo := &user_pb.PageInfo{
		Pn:    uint32(opts.Page),
		PSize: uint32(opts.PageSize),
	}
	rsp, err := u.uc.GetUserList(ctx, pageInfo)
	if err != nil {
		return nil, err
	}

	for _, data := range rsp.Data {
		userList.Items = append(userList.Items, &doUser.UserDO{
			ID:       data.Id,
			NickName: data.NickName,
			Birthday: time.Unix(int64(data.BirthDay), 0),
			Gender:   data.Gender,
			Role:     int32(data.Role),
			Mobile:   data.Mobile,
			PassWord: data.Password,
		})
	}
	userList.TotalCount = int64(rsp.Total)
	return &userList, nil
}

func NewUser(c user_pb.UserClient) userSrv.UserData {
	return &users{
		uc: c,
	}
}
